#include "mysql.h"
#include <QDebug>
MySql* MySql::sqlhand = nullptr;
MySql::MySql(QObject *parent): QObject (parent){
    initsql();
}
bool MySql::initsql(){
    if(QSqlDatabase::contains("qt_sql_default_connection")){//防止隐形内存泄漏
        database = QSqlDatabase::database("qt_sql_default_connection");
    }
    else{
        database = QSqlDatabase::addDatabase("QSQLITE");
        database.setDatabaseName("ClientDataBase.db");
        database.setUserName("root");
        database.setPassword("123456");
    }
    if(database.open()){
        QStringList tablelist = database.tables();
        sqlquery = new QSqlQuery(database);
        if(!tablelist.contains("mestable")){
            QString createusers = "CREATE TABLE mestable (name1 text,name2 text,mes text,time text)";//name1我方name2他方

            if(sqlquery->prepare(createusers)){
                   if(sqlquery->exec()){
                       qDebug()<<"Create mestable OK";
                   }

            }
            else {
                qDebug()<<"craete mestable command error";
            }
        }
        else {
            qDebug()<<"mestable already exist";
        }
    }
    else{
        qDebug()<<"database open faild!";
    }
}
void MySql::insertTable(QString name1,QString name2,QString mes,QString time){
    qDebug()<<"try to Insert"<<name1<<","<<name2<<","<<mes<<","<<time;
    QString insert = QString("INSERT INTO mestable(name1,name2,mes,time) VALUES('%1','%2','%3','%4')").arg(name1).arg(name2).arg(mes).arg(time);

    if(sqlquery->exec(insert)){

        if(1){//sqlquery->exec()){
            while(sqlquery->next()){
                qDebug()<<sqlquery->value(0).toString();
                qDebug()<<sqlquery->value(1).toString();
                qDebug()<<sqlquery->value(2).toString();
                qDebug()<<sqlquery->value(3).toString();
            }
            qDebug()<<"insert mestable OK";
        }
        else{
            qDebug()<<"insert mestable error: "<<sqlquery->lastError();
        }
    }
    else{
        qDebug()<<"insert command error";
    }
}

void MySql::dropTable(){
    tableName = "mestable";
    QString str_drop = "DROP TABLE"+tableName;
    if(sqlquery->prepare(str_drop)){
        if(sqlquery->exec()){
            qDebug()<<"drop mestable OK";
        }
    }
    else{
        qDebug()<<"drop command error";
    }
}

QString MySql::getChatRecord(QString name1,QString name2){
    //QString str = "select mes,time name from mestable where name = "+name;
    QString str = "select distinct mes,time,name1,name2 from mestable where ((name1 = "+name1+" and name2 = "+name2+")  or (name1 = "+name2+" and name2="+name1+"   ))";
    qDebug()<<str;
    QString record_str = NULL ;
    if(sqlquery->prepare(str)){
        if(sqlquery->exec()){
            qDebug()<<"select mestable OK";
            while(sqlquery->next()){
                QString mes_str = sqlquery->value(0).toString();
                QString time_str = sqlquery->value(1).toString();
                QString name_str = sqlquery->value(2).toString();
                record_str = record_str +"#" +mes_str+"#"+ time_str+"#"+ name_str;
            }
        }
    }
    else{
        qDebug()<<"select command error";

    }
    return record_str;
}
